﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Data;
using ConfigGen.Core.SettingsLoader;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.SettingsLoader
{
    [TestFixture]
    public class SpreadsheetPreferencesLoaderTests
    {
        [Test]
        public void GetSpreadsheetPreferences_NullArg()
        {
            var itemUnderTest = new SpreadsheetPreferencesLoader();
            Assert.Throws<ArgumentNullException>(() => itemUnderTest.GetSpreadsheetPreferences(null));
        }

        /// <summary>
        /// Asserts the default preferences are returned if no preferences "worksheet" exists.
        /// </summary>
        [Test]
        public void GetSpreadsheetPreferences_NoPreferencesSupplied()
        {
            var itemUnderTest = new SpreadsheetPreferencesLoader();
            var dataSet = new DataSet();

            var preferences = itemUnderTest.GetSpreadsheetPreferences(dataSet);

            Assert.IsNotNull(preferences, "Preferences: should not be null");
            var defaultPreferences = SpreadsheetPreferences.GetDefaultPreferences();
            Assert.AreEqual(defaultPreferences.EmptyStringPlaceholder, preferences.EmptyStringPlaceholder, "EmptyStringPlaceholder: default expected");
            Assert.AreEqual(defaultPreferences.NullPlaceholder, preferences.NullPlaceholder, "NullPlaceholder: default expected");
            Assert.AreEqual(defaultPreferences.NumColumnsToSkip, preferences.NumColumnsToSkip, "NumColumnsToSkip: default expected");
        }

        [Test]
        public void GetSpreadsheetPreferences_NumColumnsToIgnore()
        {
            var itemUnderTest = new SpreadsheetPreferencesLoader();
            var table = new DataTable("Preferences");
            table.Columns.Add("c1");
            table.Columns.Add("c2");
            table.Rows.Add("NumColumnsToSkip", 5);
            var dataSet = new DataSet();
            dataSet.Tables.Add(table);

            var preferences = itemUnderTest.GetSpreadsheetPreferences(dataSet);

            Assert.IsNotNull(preferences, "Preferences: should not be null");
            var defaultPreferences = SpreadsheetPreferences.GetDefaultPreferences();
            Assert.AreEqual(defaultPreferences.EmptyStringPlaceholder, preferences.EmptyStringPlaceholder, "EmptyStringPlaceholder: default expected");
            Assert.AreEqual(defaultPreferences.NullPlaceholder, preferences.NullPlaceholder, "NullPlaceholder: default expected");
            Assert.AreEqual(5, preferences.NumColumnsToSkip, "NumColumnsToSkip: incorrect");
        }

        [Test]
        public void GetSpreadsheetPreferences_EmptyStringPlaceholder()
        {
            var itemUnderTest = new SpreadsheetPreferencesLoader();
            var table = new DataTable("Preferences");
            table.Columns.Add("c1");
            table.Columns.Add("c2");
            table.Rows.Add("EmptyStringPlaceholder", "empty");
            var dataSet = new DataSet();
            dataSet.Tables.Add(table);

            var preferences = itemUnderTest.GetSpreadsheetPreferences(dataSet);

            Assert.IsNotNull(preferences, "Preferences: should not be null");
            var defaultPreferences = SpreadsheetPreferences.GetDefaultPreferences();
            Assert.AreEqual("empty", preferences.EmptyStringPlaceholder, "EmptyStringPlaceholder: incorrect");
            Assert.AreEqual(defaultPreferences.NullPlaceholder, preferences.NullPlaceholder, "NullPlaceholder: default expected");
            Assert.AreEqual(defaultPreferences.NumColumnsToSkip, preferences.NumColumnsToSkip, "NumColumnsToSkip: default expected");
        }

        [Test]
        public void GetSpreadsheetPreferences_NullPlaceholder()
        {
            var itemUnderTest = new SpreadsheetPreferencesLoader();
            var table = new DataTable("Preferences");
            table.Columns.Add("c1");
            table.Columns.Add("c2");
            table.Rows.Add("NullPlaceholder", "null");
            var dataSet = new DataSet();
            dataSet.Tables.Add(table);

            var preferences = itemUnderTest.GetSpreadsheetPreferences(dataSet);

            Assert.IsNotNull(preferences, "Preferences: should not be null");
            var defaultPreferences = SpreadsheetPreferences.GetDefaultPreferences();
            Assert.AreEqual(defaultPreferences.EmptyStringPlaceholder, preferences.EmptyStringPlaceholder, "EmptyStringPlaceholder: default expected");
            Assert.AreEqual("null", preferences.NullPlaceholder, "NullPlaceholder: incorrect");
            Assert.AreEqual(defaultPreferences.NumColumnsToSkip, preferences.NumColumnsToSkip, "NumColumnsToSkip: default expected");
        }
    }
}
